package com.byx.chatclient.widget;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ActionMenuView;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.annotation.Nullable;

import com.byx.chatclient.R;
import com.byx.chatclient.utils.GlideUtils;
import com.facebook.drawee.view.SimpleDraweeView;

import java.lang.reflect.Field;
import java.lang.reflect.Method;

/**
 * TODO: document your custom view class.
 */
public class Toolbar extends androidx.appcompat.widget.Toolbar {
    private TextView mTvTitle;
    private TextView mTvSubTitle;
    private ImageView mIvMenu;
    private SimpleDraweeView mIvLogo;
    private PopupMenu mPopupMenu;
    private OnPopupMenuListener mOnPopupMenuListener;

    public Toolbar(Context context) {
        super(context);
        init(null, 0);
    }

    public Toolbar(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(attrs, 0);
    }

    public Toolbar(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        init(attrs, defStyle);
    }

    private void init(AttributeSet attrs, int defStyle) {
        View view = LayoutInflater.from(getContext()).inflate(R.layout.main_titlebar,this,true);
        mTvTitle = view.findViewById(R.id.tv_main_toolbar_title);
        mTvSubTitle = view.findViewById(R.id.tv_main_toolbar_status);
        mIvLogo = view.findViewById(R.id.iv_main_toolbar);
        mIvMenu = view.findViewById(R.id.iv_main_toolbar_menu);
        // Load attributes
        final TypedArray a = getContext().obtainStyledAttributes(
                attrs, R.styleable.Toolbar, defStyle, 0);

        if (a.hasValue(R.styleable.Toolbar_tb_title)){
            CharSequence text = a.getText(R.styleable.Toolbar_tb_title);
            mTvTitle.setText(text);
        }
        mTvTitle.setTextSize(TypedValue.COMPLEX_UNIT_PX,a.getDimensionPixelSize(R.styleable.Toolbar_titleSize,16));
        mTvTitle.setTextColor(a.getColor(R.styleable.Toolbar_titleColor, Color.BLACK));
        if (a.hasValue(R.styleable.Toolbar_tb_subtitle)){
            CharSequence subtitle = a.getText(R.styleable.Toolbar_tb_subtitle);
            mTvSubTitle.setText(subtitle);
        }

        mTvSubTitle.setTextSize(TypedValue.COMPLEX_UNIT_PX,a.getDimensionPixelSize(R.styleable.Toolbar_subtitleSize,15));
        mTvSubTitle.setTextColor(a.getColor(R.styleable.Toolbar_subtitleColor,Color.BLACK));

        if (a.hasValue(R.styleable.Toolbar_navigationSrc)){
            mIvLogo.setImageDrawable(a.getDrawable(R.styleable.Toolbar_navigationSrc));
        }

        if (a.hasValue(R.styleable.Toolbar_menuSrc)){
//            setOverflowIcon(a.getDrawable(R.styleable.Toolbar_menuSrc));
            mIvMenu.setImageDrawable(a.getDrawable(R.styleable.Toolbar_menuSrc));
        }

        if (a.hasValue(R.styleable.Toolbar_menuRef)) {
            inflateMenu(a.getResourceId(R.styleable.Toolbar_menuRef, 0));
        }
        // Use getDimensionPixelSize or getDimensionPixelOffset when dealing with
        // values that should fall on pixel boundaries.

        a.recycle();

        setContentInsetsRelative(0,0);

    }

    /**
     * Set the title of this toolbar.
     *
     * <p>A title should be used as the anchor for a section of content. It should
     * describe or name the content being viewed.</p>
     *
     * @param resId Resource ID of a string to set as the title
     */
    @Override
    public void setTitle(int resId) {
        mTvTitle.setText(resId);
    }

    /**
     * Set the title of this toolbar.
     *
     * <p>A title should be used as the anchor for a section of content. It should
     * describe or name the content being viewed.</p>
     *
     * @param title Title to set
     */
    @Override
    public void setTitle(CharSequence title) {
        mTvTitle.setText(title);
    }

    /**
     * Set the subtitle of this toolbar.
     *
     * <p>Subtitles should express extended information about the current content.</p>
     *
     * @param resId String resource ID
     */
    @Override
    public void setSubtitle(int resId) {
        mTvSubTitle.setText(resId);
    }

    /**
     * Set the subtitle of this toolbar.
     *
     * <p>Subtitles should express extended information about the current content.</p>
     *
     * @param subtitle Subtitle to set
     */
    @Override
    public void setSubtitle(CharSequence subtitle) {
        mTvSubTitle.setText(subtitle);
    }

    /**
     * Set the icon to use for the toolbar's navigation button.
     *
     * <p>The navigation button appears at the start of the toolbar if present. Setting an icon
     * will make the navigation button visible.</p>
     *
     * <p>If you use a navigation icon you should also set a description for its action using
     * {@link #setNavigationContentDescription(int)}. This is used for accessibility and
     * tooltips.</p>
     *
     * @param resId Resource ID of a drawable to set
     *              <p>
     *              {@link androidx.appcompat.R.attr#navigationIcon}
     */
    @Override
    public void setNavigationIcon(int resId) {
        mIvLogo.setImageResource(resId);
    }

    @Override
    public void setNavigationIcon(@Nullable Drawable icon) {
        mIvLogo.setImageDrawable(icon);
    }

    public void setNavigationIcon(String localPath){
        mIvLogo.setImageURI("file://" + localPath);
    }

    /**
     * Set a listener to respond to navigation events.
     *
     * <p>This listener will be called whenever the user clicks the navigation button
     * at the start of the toolbar. An icon must be set for the navigation button to appear.</p>
     *
     * @param listener Listener to set
     * @see #setNavigationIcon(Drawable)
     */
    @Override
    public void setNavigationOnClickListener(OnClickListener listener) {
        mIvLogo.setOnClickListener(listener);
    }

    public void setOnMenuButtonClickListener(OnClickListener onClickListener){
        this.mIvMenu.setOnClickListener(onClickListener);
    }

    public void setOnPopupMenuListener(OnPopupMenuListener onPopupMenuListener) {
        this.mOnPopupMenuListener = onPopupMenuListener;
        if (mOnPopupMenuListener != null){
            mPopupMenu = mOnPopupMenuListener.initMenu();
        }
        mIvMenu.setOnClickListener(v -> {
            if (mOnPopupMenuListener != null){
                if (mPopupMenu.getAnchor() == null){
                    mPopupMenu.setLocation(mIvMenu,0,48);
                }
                mPopupMenu.showAsDropDown();
                mOnPopupMenuListener.onMenuOpened();
            }
        });
        mPopupMenu.setOnMenuItemClickListener(position -> {
            if (mOnPopupMenuListener != null){
                mOnPopupMenuListener.onMenuItemClick(position);
            }
            mPopupMenu.dismiss();
        });
        mPopupMenu.setOnMenuClosedListener(() -> {
            if (mOnPopupMenuListener != null){
                mOnPopupMenuListener.onMenuClosed();
            }
        });
    }

    public void setMenuButtonVisibility(int visibility){
        if (mIvMenu != null){
            mIvMenu.setVisibility(visibility);
        }
    }

    public void setSubTitleVisibility(int visibility){
        if (mTvSubTitle != null){
            mTvSubTitle.setVisibility(visibility);
        }
    }

    public void setAvatarVisibility(int visibility){
        if (mIvLogo != null){
            mIvLogo.setVisibility(visibility);
        }
    }

    public interface OnPopupMenuListener{
        PopupMenu initMenu();
        void onMenuOpened();
        void onMenuItemClick(int position);
        void onMenuClosed();
    }
}
